/**
 * 诊疗阶段维护初始化
 */
var SdcTherapyStage = {
    id: "SdcTherapyStageTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdcTherapyStage.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '阶段ID', field: 'id', width: 100, align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '诊疗阶段', field: 'stageName', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '诊疗阶段描述', field: 'stageDesc', width: 100, align: 'center', valign: 'middle', sortable: false},
        {title: '所属病种', field: 'entityName', width: 100, align: 'center', valign: 'middle', sortable: false},
        {title: '所属科室', field: 'linkDeptName', width: 100, align: 'center', valign: 'middle', sortable: false},
        {title: '阶段状态', field: 'isActive', width: 100, align: 'center', valign: 'middle', sortable: false,formatter:function(value,row,index) {
                if (value === 0) {
                    return "<span class='label label-warning'>作废</span>";
                } else if (value === 1) {
                    return "<span class='label label-success'>有效</span>";
                }
            }
        }
    ];
};

/**
 * 检查是否选中
 */
SdcTherapyStage.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length === 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdcTherapyStage.seItem = selected[0];
        return true;
    }
};

/**
 * 新增诊疗阶段
 */
SdcTherapyStage.add = function () {
    var index = Sys.open('新增诊疗阶段', Sys.backendPath + '/sdc/sdctherapystage/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 修改诊疗阶段详情
 */
SdcTherapyStage.edit = function (r) {
    var flag = false;
    var id = 0;
    if(r){
        id = r.id;
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('修改诊疗阶段详情', Sys.backendPath + '/sdc/sdctherapystage/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除诊疗阶段
 */
SdcTherapyStage.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function () {
            var ajax = new $ax(Sys.backendPath + "/sdc/sdctherapystage/delete", function (data) {
                Sys.success("删除成功!");
                SdcTherapyStage.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdcCdssItemStageDicId", id);
            ajax.start();
        };
        Sys.confirm("是否删除该诊疗阶段?", operation);
    }
};

/**
 * 查询诊疗阶段列表
 */
SdcTherapyStage.search = function () {
    var queryData = {};
    queryData['entityId'] = $("#entityId").combobox("getValue");
    queryData['stageName'] = $("#stageId").combobox("getText");
    queryData['stageDesc'] = $("#stageDesc").val();
    queryData['linkDept'] = $("#linkDept").combobox("getValue");
    SdcTherapyStage.table.refresh({query: queryData, pageNumber: 1});
};

$(function () {
    var defaultColunms = SdcTherapyStage.initColumn();
    var table = new BSTable(SdcTherapyStage.id, Sys.backendPath + "/sdc/sdctherapystage/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(15);
    table.setOnDblClickRow(SdcTherapyStage.edit);
    SdcTherapyStage.table = table.init();
});
